/*
 * Copyright (C) Michael Opitz
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 * USA
 */
#ifndef NETWORK_H
#define NETWORK_H

#include "generic_queue.h"
#include "../model/dom.h"
#include "../model/parser.h"
#include "../model/client.h"
#include "../model/dom_policy.h"
#include "i_socket.h"

#include <boost/shared_ptr.hpp>
#include <map>

#include <iostream> 
using namespace std;

/** 
 * \brief This namespaces includes all nertwork related stuff.
 * \author Michael Opitz
 */
namespace network {
	class event_handler;
	typedef unsigned int event_type;
	/** 
	 * \brief These are all possible event-types which are inserted by the
	 * receiver and connection_listener into the message_queue.
	 */
	enum {
		CONNECT = 0,
		RECEIVE = 2,
		DELETE = 4
	};

	/** 
	 * \brief This class represents messages which are inserted by the
	 * receiver and connection_listener into the message_queue.
	 */
	struct message {
		/** 
		 * \brief This is the socket, from which the message was
		 * received.
		 */
		boost::shared_ptr<network::i_socket> sock;
		/** 
		 * \brief If this message is a CONNECT-message, this socket is
		 * the socket from the established connection.
		 */
		boost::shared_ptr<network::i_socket> new_sock;
		/** 
		 * \brief If this message is a RECEIVE-message, this object
		 * contains the received dom-node.
		 */
		boost::shared_ptr<model::dom> node;
		/** 
		 * \brief This object indicates the type of this message.
		 */
		network::event_type type;
	};

	struct event {
		event(boost::shared_ptr<model::dom> n, 
		      boost::shared_ptr<network::event_handler> h, 
		      boost::shared_ptr<network::i_socket> s)
		:node(n), handler(h), new_sock(s){

		}
		boost::shared_ptr<model::dom> node;
		boost::shared_ptr<network::event_handler> handler;
		boost::shared_ptr<network::i_socket> new_sock;
	};

	typedef generic_queue<network::message> message_queue;
	typedef generic_queue<network::event> event_queue;
	typedef model::parser<model::dom_policy> dom_parser;

	typedef std::map<boost::shared_ptr<network::i_socket>, model::client*>
		network_client;
}

#endif
